/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('service.auth', [
        "ngResource",
        "ngStorage"
    ])
    //认证服务
    .service('MallAuthService', ['$resource', '$localStorage', function ($resource, $localStorage) {
        var $storage = $localStorage.$default({
            name: '',
            userId: '',
            wxpay_openid: '',
        });

        var userpoint=$resource(mall_uc + 'userpoints/byorderid/:userid');
        // 已登录则返回userid，否则跳转指定地址
        this.ensure_login = function () {
            if ($storage.userId) {
                return $storage.userId;
            }
            alert("您还未登录,请先登录")
            window.location.href = projectBaseUrl + "login/";
        }

        this.getUserid = function () {
            return $storage.userId;
        }

        this.getUsername = function () {
            return $storage.name;
        }
        //获取个人账户信息
        this.Userbalance = function () {
            var id = this.getUserid();
            var userbal = $resource(mall_uc + "userbalance/:userid");
            return userbal.get({userid: id});
        }

        this.getWxopenid = function () {
            return $storage.wxpay_openid;
        }

        //加入服务器购物车
        this.addToCartByUserId = function (goods, success, error) {
            var addToCart = $resource(mallorder + "cart");
            return addToCart.save(goods, success, error)
        }
        //修改服务器购物车
        this.updateCartByCartId = function (goods, success, error) {
            var updateCart = $resource(mallorder + "cart/:id");
            return updateCart.save({id: goods.cartid}, goods, success, error);
        }
        //根据buyid获取接口
        this.getShopCartMessage = function () {
            var userId = this.getUserid();
            var shopCarbuyidmessg = $resource(mallorder + "cart/bybuyerid/:buyerid");
            return shopCarbuyidmessg.query({buyerid: userId})
        }

        //用户获取积分积分接口
        this.postuserpoint=function(order,success){
            return userpoint.save({userid:order.buyerid,orderid:order.orderid},order,success);
        }
        //获取积分
        this.getUserpoints =function(){
            var userid = this.getUserid();
            var Userpoints = $resource(mall_uc +"userpoints/:id")
            return Userpoints.get({id:userid})
        }
        //通过用户积分兑换金钱
        this.exchangeMoneyByPoints = function(points,success,error){
            var userid = this.getUserid();
            var MoneyByPoints = $resource(mall_uc +"userpoints/pointsconvert/money/:id")
            return MoneyByPoints.save({id:userid,points:points},{},success,error)
        }
    }])

    //用户地址服务
    .service('MallAddressService', ['$resource', 'MallAuthService', function ($resource, MallAuthService) {

        var address = $resource(mall_uc + 'usercontact/byuserid/:userid');
        //获取该用户的所有地址
        this.alladdress = function () {
            var userid = MallAuthService.getUserid();
            return address.query({userid: userid});
        }
        //获取地址
        this.get_address = function () {
            if (userid != '') {
                return UserContactsByUserid.query({userid: userid});
            }
        }
        //删除收货地址
        this.remove = function (contactid) {
            var resource = $resource(mall_uc + "usercontact/:id");
            return resource.remove({id: contactid});
        }

        this.update = function (contactid) {
            var resource = $resource(mall_uc + "usercontact/byuserid/setdefault/:userid");
            return resource.get({userid: userid, contactid: contactid});
        }
        //修改收货地址
        this.revise = function (data) {
            var resource = $resource(mall_uc + "usercontact/:id");
            return resource.save({id: data.contactid}, data);
        }
    }])
;
